package flc.ast.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.PicEditActivity;
import flc.ast.adapter.DraftAdapter;
import flc.ast.adapter.RecordAdapter;
import flc.ast.databinding.FragmentEditBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link EditFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class EditFragment extends BaseNoModelFragment<FragmentEditBinding> {

    private RecordAdapter recordAdapter;
    private List<String> listShow = new ArrayList<>();


    @Override
    protected int onCreate() {
        return R.layout.fragment_edit;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivPicFilter.setOnClickListener(this);
        mDataBinding.ivPicSticker.setOnClickListener(this);
        mDataBinding.ivPicCrop.setOnClickListener(this);
        mDataBinding.ivPicDoodle.setOnClickListener(this);
        mDataBinding.ivPicText.setOnClickListener(this);
        mDataBinding.ivPicAdjust.setOnClickListener(this);
        mDataBinding.ivClear.setOnClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new GridLayoutManager(mContext, 2));
        recordAdapter = new RecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPicFilter://滤镜
                getPermission(1);
                break;
            case R.id.ivPicSticker://贴纸
                getPermission(2);
                break;
            case R.id.ivPicCrop://裁剪
                getPermission(0);
                break;
            case R.id.ivPicDoodle://涂鸦
                getPermission(4);
                break;
            case R.id.ivPicText://文字
                getPermission(3);
                break;
            case R.id.ivPicAdjust://调节
                getPermission(5);
                break;
            case R.id.ivClear://清除
                clear();
                break;

        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecord;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        if (listShow.size() > 0) {
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
        }
    }

    private void getPermission(int position) {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        goToEdit(position);
                    }
                }).request();
    }

    private void goToEdit(int position) {
        PhotoSelectorBuilder.builder(getActivity())
                .mode(PhotoConfig.Mode.PHOTO)
                .minCount(1)
                .maxCount(1)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        ArrayList<String> pathList = new ArrayList<>();
                        for (SelectMediaEntity entity : data) {
                            pathList.add(entity.getTargetPath());
                        }
                        PicEditActivity.pos = position;
                        PicEditActivity.sImgPath = pathList.get(0);
                        startActivity(PicEditActivity.class);
                    }
                });
    }

    private void clear() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        //从此文件夹删除
                        for (String s : listShow) {
                            FileUtils.delete(s);
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        getData();
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.clear_suc));
                    }
                });
            }
        }, 1000);
    }

}